
import java.io.*;
import java.util.*;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;

class Solution {
    public void myFunc(int raw) {
        // 使用自测数据按钮时调试用，正式提交时要删掉。
      //  int i = arr.get(0).get(0);
        if(raw >= 999000){
            System.out.println(999999);
            return;
        }
        int numHigh = raw/1000;
        int sumHigh = (numHigh/10)%10+numHigh/100+numHigh%10;
        int numLow = raw-numHigh*1000;
        int sumLow = (numLow/10)%10+numLow/100+numLow%10;

        if(sumHigh == sumLow){
            int low1 = numLow/100;
            int low2 = (numLow/10)%10;
            int low3 = numLow%10;

            List<Integer> newLow = new ArrayList<>();
            newLow.add(low1*100+low2+low3*10);
            newLow.add(low2*100+low3+low1*10);
        }

        if(sumHigh>sumLow){
            int intveral = sumHigh-sumLow;
            numLow = numLow+intveral;
            int sumLow2= (numLow/10)%10+numLow/100+numLow%10;
            //不考虑进位
            if(sumHigh==sumLow2){
                System.out.println(numHigh*1000+numLow);
                return;
            }
        }
        System.out.println(10*10*10*10*10*10-2);
    }
}
public class TestCin
{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        int raw = 0;
     //   ArrayList<ArrayList<Integer>> arr = new ArrayList<ArrayList<Integer>>();
        int lineNum = 0;
        while(cin.hasNextLine())
        {
           ArrayList<Integer> row = new ArrayList<Integer>();
            String line = cin.nextLine();
            if (line.length() > 0) {
                raw = Integer.parseInt(line);
                String[] arrLine = line.split(" ");
                for (int i=0; i<arrLine.length; i++) {
                    row.add(Integer.parseInt(arrLine[i]));
                }
              //  arr.add(row);
            }
            lineNum++;
            if(lineNum>=1){
                break;
            }
        }

        new Solution().myFunc(raw);
    }
}